<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="请输入检索信息" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.type" placeholder="区域" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <!--      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">-->
      <!--        <template slot-scope="{row}">-->
      <!--          <span>{{ row.id+1 }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="项目编号" width="250px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleUpdate(row)">{{ row.value }}</span>
          <el-tag>{{ row.type | typeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="安装人姓名" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.install_person }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装人电话" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.install_phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合作银行" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.bank | bankFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status | stateFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            查看
          </el-button>

          <!--          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">-->
          <!--            Delete-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 600px; margin-left:150px;">
        <el-form-item label="项目名称" prop="title">
          <el-input :value="temp.value" />
        </el-form-item>
        <el-form-item label="图片信息">
          <el-image
            style="width: 100px; height: 100px"
            :src="url"
            :preview-src-list="srcList"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          返回
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchPv, createArticle, updateArticle } from '@/api/article'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { getDeviceList } from '@/api/device'
import { DelSubject, getImageList } from '@/api/subject'
import { scrollTo } from '@/utils/scroll-to'

// 区域信息
const calendarTypeOptions = [
  { key: '371601', display_name: '市直管' },
  { key: '371602', display_name: '滨城区' },
  { key: '371604', display_name: '开发区' },
  { key: '371605', display_name: '高新区' },
  { key: '371606', display_name: '北海新区' },
  { key: '371621', display_name: '惠民县' },
  { key: '371622', display_name: '阳信县' },
  { key: '371623', display_name: '无棣县' },
  { key: '371624', display_name: '沾化区' },
  { key: '371625', display_name: '博兴县' },
  { key: '371626', display_name: '邹平市' },
  { key: '371627', display_name: '工业园区' }
]
// 状态信息
const calendarStateOptions = [
  { key: '0', display_name: '停工' },
  { key: '1', display_name: '开工' },
  { key: '2', display_name: '完工' },
  { key: '3', display_name: '筹建' },
  { key: '4', display_name: '立项' }
]
// 合作银行信息
const calendarBankOptions = [
  { key: '0', display_name: '农商银行' },
  { key: '1', display_name: '邮储银行' },
  { key: '2', display_name: '工商银行' },
  { key: '3', display_name: '恒丰银行' },
  { key: '4', display_name: '兴业银行' },
  { key: '5', display_name: '中国银行' },
  { key: '6', display_name: '东营银行' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
// 获取项目的状态信息
const calendarStateKeyValue = calendarStateOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
// 获取合作银行的信息
const calendarBankKeyValue = calendarBankOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        2: 'info',
        0: 'danger',
        3: 'warning',
        4: 'error'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    },
    stateFilter(type) {
      return calendarStateKeyValue[type]
    },
    bankFilter(type) {
      return calendarBankKeyValue[type]
    }
  },
  data() {
    return {
      //  图片信息
      url: 'http://8.141.50.32//upload/20210114/3ac4bab2a6e26696476e343cbbfbfcbb.png',
      srcList: [
        'http://8.141.50.32//upload/20210114/3ac4bab2a6e26696476e343cbbfbfcbb.png'
      ],
      // 文件列表
      fileList: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        value: undefined,
        type: undefined,
        sort: '+id'
      },
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      statusOptions: [''],
      calendarStateOptions: [],
      showReviewer: false,
      temp: {
        id: undefined,
        num: '',
        bank: '',
        timestamp: new Date(),
        value: '',
        type: '',
        status: '',
        imageurl: '',
        install_person:'',
        install_phone:''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改项目状态',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }],
        bank: [{ required: true, message: 'title is required', trigger: 'blur' }]

      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 文件上传
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
      this.image = ''
      for (const i in fileList) {
        this.image += fileList[i]['response'] + ';'
      }
    },
    handlePreview(file) {
      console.log(file)
    },
    handleSuccess(response, file, fileList) {
      this.image = ''
      for (const i in fileList) {
        this.image += fileList[i]['response'] + ';'
      }
    },

    getList(val) {
      this.listLoading = true
      if (val != undefined) {
        this.page = val.page,
        this.limit = val.limit
      }
      this.type = this.listQuery.type
      this.title = this.listQuery.title
      getDeviceList(
        {
          page: this.page,
          limit: this.limit,
          subject_area: this.type,
          subject_name: this.title
        }).then((res) => {
        if (res.code == '20000') {
          this.list = res.data
          // console.log(this.list)
          this.total = res.msg
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)

          // this.$message('提交成功!')
        } else {
          this.$message({
            message: '提交失败，请联系管理员!',
            type: 'error'
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      console.log(this.temp)
      getImageList(
        {
          subject_num: this.temp.num
        }).then((res) => {
        if (res.code == '20000') {
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)
          this.url = res.msg
          this.srcList = res.data
        } else {
          this.$message({
            message: '提交失败，请联系管理员!',
            type: 'error'
          })
        }
      }).catch(err => {
        console.log('err', err)
      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },

    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: '',
        type: ''
      }
    },

    handleDelete(row, index) {
      console.log(row.num, index)
      DelSubject(
        {
          subject_num: row.num
        }).then((res) => {
        if (res.code == '20000') {
          // console.log(this.list)
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)

          //  this.$message('修改成功!')
          if (res.data == 0) {
            this.$notify({
              title: '信息',
              message: '已删除，无需重复操作',
              type: 'warning',
              duration: 2000
            })
          } else {
            this.$notify({
              title: '信息',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }
        } else {
          this.$message({
            message: '删除失败，请联系管理员史岩松!',
            type: 'error'
          })
        }
      })
      this.list.splice(index, 1)
    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    }

  }
}
</script>
